package hk.edu.cityu.is.riskmgmt.applets;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JApplet;
import javax.swing.Timer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class CoFApplet extends JApplet {

	private static final long serialVersionUID = 2594874473564639267L;
	private double lastValue_fx_ratio;
	private double lastValue_ir_ratio;
	private double lastValue_ta_ratio;
	private double lastValue_cash_ratio;
	private int tick;

	class CoFDataGenerator extends Timer implements ActionListener {

		/**
		 * 
		 */
		private static final long serialVersionUID = -1103449238324643503L;

		public void actionPerformed(ActionEvent actionevent) {
			try {

				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				String connectionUrl = "jdbc:sqlserver://localhost:1438;database=HKBANK;user=sa;password=1111";
				Connection conn = DriverManager.getConnection(connectionUrl);
				Statement stmt = conn.createStatement(
						ResultSet.TYPE_SCROLL_SENSITIVE,
						ResultSet.CONCUR_UPDATABLE);
				String sql1 = "select top 1 miles from dbo.last order by id desc";
				ResultSet rs = stmt.executeQuery(sql1);
				rs.next();
				long miles = rs.getLong("miles");
				String sql = "select * from dbo.last where miles=" + miles;
				// System.out.println(sql);
				rs = stmt.executeQuery(sql);
				// System.out.println(rs.getInt("miles"));
				while (rs.next()) {
					lastValue_fx_ratio = rs.getDouble("cof_fx");
					lastValue_ir_ratio = rs.getDouble("cof_ir");
					lastValue_cash_ratio = rs.getDouble("cof_cash");
					lastValue_ta_ratio = rs.getDouble("cof_ta");
					tick = rs.getInt("tick");
					System.out.println("lastValue_fx_ratio: "
							+ lastValue_fx_ratio + ", lastValue_ir_ratio:"
							+ lastValue_ir_ratio + ", lastValue_cash_ratio:"
							+ lastValue_cash_ratio + ", lastValue_ta_ratio"
							+ lastValue_ta_ratio);
					addFXObservation(tick, lastValue_fx_ratio);
					addIRObservation(tick, lastValue_ir_ratio);
					addCashObservation(tick, lastValue_cash_ratio);
					addTAObservation(tick, lastValue_ta_ratio);

				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// System.out.println(lastValue1);

			// addFreeObservation(lastValue2);
		}

		CoFDataGenerator() {
			super(100, null);
			addActionListener(this);
		}
	}

	public CoFApplet() {
		// lastValue1 = 100D;

		fx = new XYSeries("FX");
		ir = new XYSeries("IR");
		cash = new XYSeries("Cash");
		tradingassets = new XYSeries("Trading assets");
		// total.setHistoryCount(30000);
		// free = new TimeSeries("Cpu01",
		// org.jfree.data.time.Millisecond.class);
		// free.setHistoryCount(30000);

		XYSeriesCollection xyseriecollection = new XYSeriesCollection();
		xyseriecollection.addSeries(fx);
		xyseriecollection.addSeries(ir);
		xyseriecollection.addSeries(tradingassets);
		xyseriecollection.addSeries(cash);
		// timeseriescollection.addSeries(free);
		// BasicStroke basicstroke = new BasicStroke(2.0F, 0, 2);

		// DateAxis horizontaldateaxis = new DateAxis("Time");
		// NumberAxis verticalnumberaxis = new NumberAxis("Value");

		XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(
				true, false);

		xylineandshaperenderer.setSeriesPaint(0, Color.red);
		xylineandshaperenderer.setSeriesPaint(1, Color.green);
		// xylineandshaperenderer.setStroke(new BasicStroke(3F, 0, 2));

		JFreeChart jfreechart = ChartFactory.createXYLineChart(
				"Concentration of Funding", "Tick", "Value", xyseriecollection,
				PlotOrientation.VERTICAL, true, true, false);

		XYPlot xyplot = jfreechart.getXYPlot();
		ValueAxis valueaxis = xyplot.getDomainAxis();
		valueaxis.setTickLabelsVisible(true);
		valueaxis.setAutoRange(true);
		// valueaxis.setFixedAutoRange(60000D);
		valueaxis = xyplot.getRangeAxis();
		// valueaxis.setTickLabelsVisible(false);
		valueaxis.setAutoRange(true);
		// valueaxis.setFixedAutoRange(20);
		ChartPanel chartpanel = new ChartPanel(jfreechart);
		chartpanel.setPopupMenu(null);
		getContentPane().add(chartpanel);
		(new CoFDataGenerator()).start();
	}

	private void addFXObservation(int tick, double d) {

		fx.add(tick, d);
	}

	private void addIRObservation(int tick, double d) {

		ir.add(tick, d);
	}

	private void addTAObservation(int tick, double d) {

		tradingassets.add(tick, d);
	}

	private void addCashObservation(int tick, double d) {

		cash.add(tick, d);
	}

	private XYSeries fx;
	private XYSeries ir;
	private XYSeries tradingassets;
	private XYSeries cash;

}